﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;

namespace KentekenHerkenning
{
    class HTMLformatter
    {

        public string HTML;
        public int TotalTime;
        public int TotalPercentage;
        public int AverageDistance;
        public int CorrectPlates = 0;
        public int badPlatesPercentage;
        public int badPlatesDifference;
        public int badPlates;
        
        public bool Format(List<Result> Results)
        {

            HTML = "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">\r\n"
                 + " <html xmlns=\"http://www.w3.org/1999/xhtml\">\r\n"
                 + " <head>\r\n"
                 + " <meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" />\r\n"
                 + " <title>TestResultaten</title>\r\n"
                 + " <style type=\"text/css\">\r\n"
                 + " <!--\r\n"
                 + " table.MsoTableLightShadingAccent1 {\r\n"
                 + " border-top:solid #4F81BD 1.0pt;\r\n"
                 + " border-left:none;\r\n"
                 + " border-bottom:solid #4F81BD 1.0pt;\r\n"
                 + " border-right:none;\r\n"
                 + " text-autospace:ideograph-other;\r\n"
                 + " font-size:12.0pt;\r\n"
                 + " font-family:\"Times New Roman\",\"serif\";\r\n"
                 + " color:#365F91;\r\n"
                 + " }\r\n"
                 + " p.MsoNormal {\r\n"
                 + " margin:0cm;\r\n"
                 + " margin-bottom:.0001pt;\r\n"
                 + " text-autospace:ideograph-other;\r\n"
                 + " font-size:12.0pt;\r\n"
                 + " font-family:\"Times New Roman\",\"serif\";\r\n"
                 + " }\r\n"
                 + " .TopBar {\r\n"
                 + " 	\r\n"
                 + " 	width:176.95pt;\r\n"
                 + " 	border-top:solid #4F81BD 1.0pt;\r\n"
                 + " 	border-left:none;border-bottom:solid #4F81BD 1.0pt;\r\n"
                 + " 	border-right:none;\r\n"
                 + " 	padding:0cm 5.4pt 0cm 5.4pt;\r\n"
                 + " 	color:#365F91;\r\n"
                 + " }\r\n"
                 + " .BackgroundWhite{\r\n"
                 + " 	width:176.95pt;\r\n"
                 + " 	border:none;\r\n"
                 + " 	background:#FFFFFF;\r\n"
                 + " 	padding:0cm 5.4pt 0cm 5.4pt;\r\n"
                 + " }\r\n"
                 + " .BackgroundBlue{\r\n"
                 + " 	width:176.95pt;\r\n"
                 + " 	border:none;\r\n"
                 + " 	background:#D3DFEE;\r\n"
                 + " 	padding:0cm 5.4pt 0cm 5.4pt;\r\n"
                 + " 	\r\n"
                 + " }\r\n"
                 + " .ColorNormal{\r\n"
                 + " 	color:#365F91;\r\n"
                 + " }\r\n"
                 + " .ColorGreen{\r\n"
                 + " 	color:#00B050;\r\n"
                 + " }\r\n"
                 + " -->\r\n"
                 + " </style>\r\n"
                 + " </head>\r\n"
                 + " <body>\r\n"
                 + " <table class=\"MsoTableLightShadingAccent1\" border=\"1\" cellspacing=\"0\" cellpadding=\"0\" style=\"border-collapse:collapse;border:none;\">\r\n"
                 + " <tr>\r\n"
                 + " <td width=\"250\" valign=\"top\" class=\"TopBar\" ><p class=\"MsoNormal\"><strong>Kenteken</strong></p></td>\r\n"
                 + " <td width=\"250\" valign=\"top\" class=\"TopBar\" ><p class=\"MsoNormal\"><strong>Gevonden door OCR</strong></p></td>\r\n"
                 + " <td width=\"250\" valign=\"top\" class=\"TopBar\" ><p class=\"MsoNormal\"><strong>Locatie Verschil</strong></p></td>\r\n"
                 + "<td width=\"248\" valign=\"top\" class=\"TopBar\" ><p class=\"MsoNormal\"><strong>Tijd (ms)</strong></p></td>\r\n"
                 + " </tr>";
            bool odd = true;
            string style = "BackgroundBlue";
            
            foreach (Result r in Results)
            {
                if (r.foundLicensePlate.Length < 4)
                {
                    badPlates++;
                    badPlatesDifference += r.CalcDifference();
                    badPlatesPercentage += r.CorrectPercentage;
                    

                }
                if (r.foundLicensePlate == r.supposedLicencePLate)
                {
                    CorrectPlates++;
                }
                TotalTime += r.Time;
                TotalPercentage += r.CorrectPercentage;
                AverageDistance += r.CalcDifference();
                if (odd)
                {
                    style = "BackgroundBlue";
                }
                else
                {
                    style = "BackgroundWhite";
                }
                odd = !odd;
                


                HTML += "<tr>\r\n"
+" <td width=\"250\" valign=\"top\" class=\""+style+ "\" ><p class=\"MsoNormal\"><strong><span style=\"color:#365F91; \"><img width=\"213\" height=\"54\" src=\""+r.filePath+"\" /></span><span style=\"color:#365F91; \"> </span></strong></p></td>\r\n"
+ " <td width=\"250\" valign=\"top\" class=\"" + style + "\" >"+ OCRstring(r.foundLicensePlate,r.supposedLicencePLate) +" </td>\r\n"
+" <td width=\"250\" valign=\"top\" class=\""+style+ "\" ><span >"+r.CalcDifference()+" px</span></td>\r\n"
+ " <td width=\"248\" valign=\"top\" class=\"" + style + "\" ><span class=\"ColorNormal\">"+r.Time+"</span></td> </tr>";
            }

            TotalPercentage /= Results.Count;
            AverageDistance /= Results.Count;



            HTML+= "<tr>\r\n"
+ " <td width=\"250\" valign=\"top\" class=\"TopBar\" ><p class=\"MsoNormal\"><strong>Kentekens Juist</strong></p></td>\r\n"
+ " <td width=\"250\" valign=\"top\" class=\"TopBar\" ><p class=\"MsoNormal\"><strong>Percentage letters gevonden</strong></p></td>\r\n"
+ " <td width=\"250\" valign=\"top\" class=\"TopBar\" ><p class=\"MsoNormal\"><strong>Gemiddeld Verschil</strong></p></td>\r\n"
+ "<td width=\"248\" valign=\"top\" class=\"TopBar\" ><p class=\"MsoNormal\"><strong>Totale Tijd (ms)</strong></p></td>\r\n"
+ " </tr>\r\n"
+ " <tr>\r\n"
+ " <td width=\"250\" valign=\"top\" class=\"BackgroundBlue\" >"+CorrectPlates+" </td>\r\n"
+ " <td width=\"250\" valign=\"top\" class=\"BackgroundBlue\">"+TotalPercentage+"</td>\r\n"
+ " <td width=\"250\" valign=\"top\" class=\"BackgroundBlue\">"+AverageDistance+" px</td>\r\n"
+ " <td width=\"248\" valign=\"top\" class=\"BackgroundBlue\">"+TotalTime+"</td> </tr>\r\n"
+ " \r\n";

            TotalPercentage *= Results.Count;
            TotalPercentage -= badPlatesPercentage;
            TotalPercentage /= Results.Count - badPlates;
            AverageDistance *= Results.Count;
            AverageDistance -= badPlatesDifference;
            AverageDistance /= Results.Count - badPlates;

            HTML += "<tr>\r\n"
            + " <td width=\"250\" valign=\"top\" class=\"TopBar\" ><p class=\"MsoNormal\"><strong>Kentekens Goed gelokaliseerd</strong></p></td>\r\n"
            + " <td width=\"250\" valign=\"top\" class=\"TopBar\" ><p class=\"MsoNormal\"><strong>Percentage letters gevonden mits goed gelokaliseerd</strong></p></td>\r\n"
            + " <td width=\"250\" valign=\"top\" class=\"TopBar\" ><p class=\"MsoNormal\"><strong>Gemiddeld Verschil mits goed gelokaliseerd</strong></p></td>\r\n"
            + "<td width=\"248\" valign=\"top\" class=\"TopBar\" ><p class=\"MsoNormal\"><strong></strong></p></td>\r\n"
            + " </tr>\r\n"
            + " <tr>\r\n"
            + " <td width=\"250\" valign=\"top\" class=\"BackgroundBlue\" >" + (Results.Count-badPlates) + " </td>\r\n"
            + " <td width=\"250\" valign=\"top\" class=\"BackgroundBlue\">" + TotalPercentage + "</td>\r\n"
            + " <td width=\"250\" valign=\"top\" class=\"BackgroundBlue\">" + AverageDistance + " px</td>\r\n"
            + " <td width=\"248\" valign=\"top\" class=\"BackgroundBlue\"></td> </tr>\r\n"
            + " \r\n"
            + " </table>\r\n"
            + " </body>\r\n"
            + " </html>";




            SaveToFile(System.Reflection.Assembly.GetExecutingAssembly().Location.Replace("FinalKentekenHerkenning.exe", "") + "Results.html");



            return true;
        }


        private string OCRstring(string foundLP, string supposedLP)
        {
            string s = "";
            char[] found = foundLP.ToCharArray();
            char[] supposed = supposedLP.ToCharArray();
            try
            {
                for (int i = 0; i < found.Length; i++)
                {
                    if (found[i] == supposed[i])
                    {
                        s += "<span class=\"ColorGreen\" >" + found[i] + "</span>";
                    }
                    else
                    {
                        s += found[i];
                    }

                    if (i == 1)
                    {

                        s += "-";
                    }
                    if (i == 3)
                    {
                        if (Char.IsLetter(found[4]) == Char.IsLetter(found[5]))
                        {

                            s += "-";

                        }

                    }

                    if (i == 4 && (Char.IsLetter(found[4]) != Char.IsLetter(found[5])))
                    {

                        s += "-";
                    }

                }
            }
            catch { }
            


            return s;
        }

        public void SaveToFile(string filename)
        {
            System.IO.StreamWriter file = new System.IO.StreamWriter(filename);
            file.WriteLine(HTML);

            file.Close();


        }

        

    }
}
